package com.tianhui.consignortwofei.mvp.ui.activity.deliverGoods;

import android.os.Bundle;
import android.text.TextUtils;

import com.fgs.common.CommonResponse;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.LogUtil;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.contract.GoodsContract;
import com.tianhui.consignortwofei.mvp.model.OrderModel;
import com.tianhui.consignortwofei.mvp.model.enty.GoodsInfo;
import com.tianhui.consignortwofei.other.DeliverGoodsDataProcess;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fgs
 * @date 2020/9/1
 * @description
 **/
public abstract class BaseDeliverGoodsActivity extends BaseActivity<GoodsContract.View,GoodsContract.Presenter> implements GoodsContract.View {


    protected GoodsInfo mGoodsInfo;
    protected OrderModel mOrderModel;
    protected DeliverGoodsDataProcess mDataProcess;

    @Override
    public void init() {
        mOrderModel = new OrderModel();
        mDataProcess = new DeliverGoodsDataProcess();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            String goodsId = bundle.getString(KeyValue.KEY_ID);
            if (!TextUtils.isEmpty(goodsId)) {
                requestGoodsDetail(goodsId);
            } else {
                mGoodsInfo = bundle.getParcelable(KeyValue.KEY_GOODS_INFO);
                LogUtil.print(JsonUtil.toJson(mGoodsInfo));
            }

        }
        if (mGoodsInfo == null) {
            mGoodsInfo = new GoodsInfo();
        }

        initData();
        initView();
        initListener();
    }

    public abstract void initData();

    public abstract void initView();

    public abstract void initListener();

    private void requestGoodsDetail(String goodsId) {
        Map<String, String> map = new HashMap<>();
        map.put("id", goodsId);
        mOrderModel.orderDetailModel(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    GoodsInfo goodsInfo = JsonUtil.fromJson(JsonUtil.toJson(commonResponse.getData()), GoodsInfo.class);
                    if (goodsInfo != null) {
                        mGoodsInfo = goodsInfo;
                        initData();
                        initView();
                    } else {
                        showToast("未找到货源信息");
                    }
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }


    @Override
    public String getToolbarTitle() {
        return "发布货源";
    }

    protected void openNextStepActivity(Class pClass) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(KeyValue.KEY_GOODS_INFO, mGoodsInfo);
        openActivity(pClass, bundle);
    }
}
